package databaseConnect;

import java.util.HashMap;
import java.util.Vector;

import datatypes.CommonConstants;
import datatypes.Host;
import datatypes.HostDescription;
import datatypes.ScenarioActionDescription;
import datatypes.ScenarioDescription;

public class SimulationExecutionRequestProcessor {

	public static HashMap<String, String> getAvailableSimulations() {
		Vector<Vector <String>> tmp = AccessConnect.executeSelect("SELECT ID, Simulationname FROM SimulationDescription");
		HashMap<String, String> idNameToidString = new HashMap<String, String>();
		for (int i= 0 ; i < tmp.size(); i++ ){
			idNameToidString.put(tmp.get(i).get(0) + " " + tmp.get(i).get(1), tmp.get(i).get(0));
		}	
		return idNameToidString;	
	}
	
	public static Vector<String> getScenariosForSimulation(int SimulationID){
		Vector<Vector <String>> tmp = AccessConnect.executeSelect("SELECT ScenarioID FROM SimulationContent WHERE SimulationID=" + SimulationID + " ORDER BY ScenarioID");
		Vector<String> tmptmp = new Vector<String>();
		for (int i= 0 ; i < tmp.size(); i++ ){
			tmptmp.add(tmp.get(i).get(0));
		}
		return tmptmp;		
	}
	
	public static Vector<String> getScenarioNamesForSimulation(int SimulationID){
		Vector<Vector <String>> tmp = AccessConnect.executeSelect("SELECT myID, myScenarioName, mySubname FROM (SELECT t.ID as myID , t.ScenarioName as myScenarioName, t.Subname as mySubname, b.ScenarioID, b.SimulationID FROM SimulationContent as b, ScenarioDescriptions as t WHERE SimulationID=" + SimulationID + "AND b.ScenarioID = t.ID) ORDER BY myID");
		Vector<String> tmptmp = new Vector<String>();
		for (int i= 0 ; i < tmp.size(); i++ ){
			tmptmp.add(tmp.get(i).get(0) + " - " + tmp.get(i).get(1) + " - " + tmp.get(i).get(2) + " - " );
		}
		return tmptmp;		
	}

	public static Vector<Vector<String>> getScenarioParameterRows (int id){
		Vector<String> names = AccessConnect.getColumnNamesForSelect("SELECT * FROM ScenarioDescriptions");
		Vector<Vector<String>> rows = AccessConnect.executeSelect("SELECT * FROM ScenarioDescriptions WHERE ID=" + id);
		Vector<Vector<String>> pivot = new Vector<Vector<String>> ();
		for (int i = 0 ; i < names.size(); i++){
			Vector<String> tmptmp = new Vector<String>();
			//System.out.println(names.get(i) +"blub" + rows.firstElement().get(i));
			tmptmp.add(names.get(i));
			tmptmp.add(rows.firstElement().get(i));
			pivot.add(tmptmp);
		}
		return pivot;
	}
	
	public static ScenarioDescription getScenarioDescription (int ScenarioID, int SimulationID){
		Vector<Vector<String>> tmp = getScenarioParameterRows (ScenarioID);
		ScenarioDescription scenDesc = new ScenarioDescription();
		try {
			scenDesc.setID(ScenarioID);
			scenDesc.setScenarioName(tmp.get(1).get(1));
			scenDesc.setSubname(tmp.get(2).get(1));
			scenDesc.setDescription(tmp.get(3).get(1));
			scenDesc.setRosterWidth(Integer.parseInt(tmp.get(4).get(1)));
			scenDesc.setRosterHeight(Integer.parseInt(tmp.get(5).get(1)));
			scenDesc.setRange(Integer.parseInt(tmp.get(6).get(1)));
			scenDesc.setHosts(getHosts(ScenarioID, SimulationID));
			scenDesc.setActions(getActions(ScenarioID));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return scenDesc;
	}

	public static Vector<Vector<String>> getHostRows (int Id){
		return AccessConnect.executeSelect("SELECT HostID FROM StartConfigurationNodes WHERE ScenarioID= " + Id + " ORDER BY HostID ASC");
	}
	
	private static Vector<HostDescription> getHosts (int ScenarioID, int SimulationID){
		Vector<Vector<String>> rows = getHostRows(ScenarioID);
		Vector<HostDescription> hosts = new Vector<HostDescription>();
		try {
			for (int i = 0; i < rows.size(); i++){
				Vector<Vector<String>> singleHost = AccessConnect.executeSelect("SELECT * FROM HostDescription WHERE ID=" + rows.get(i).get(0));
				Vector<Vector<String>> isActive = AccessConnect.executeSelect("SELECT Active FROM StartConfigurationNodes WHERE ScenarioID=" + ScenarioID + "AND HostID=" + rows.get(i).get(0));
				Host host = new Host(rows.get(i).get(0),isActive.firstElement().firstElement().equals("1") ,getServicesForHost(Integer.parseInt(rows.get(i).get(0))),SimulationID);
				host.setScenarioID(ScenarioID);
				HostDescription hostdesc = new HostDescription(host, Integer.parseInt(singleHost.firstElement().get(1)), Integer.parseInt(singleHost.firstElement().get(2)), isActive.firstElement().firstElement());
				hosts.add(hostdesc);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hosts;
	}
	
	
	private static Vector<ScenarioActionDescription> getActions (int ScenarioID){
		Vector<Vector<String>> rows = AccessConnect.executeSelect("SELECT * FROM ActionQueue WHERE ScenarioID=" + ScenarioID + " ORDER BY Position ASC");
		Vector<ScenarioActionDescription> actions = new Vector<ScenarioActionDescription>();
		for (int i = 0; i < rows.size(); i++){
			Vector<Vector<String>> singleAction = AccessConnect.executeSelect("SELECT * FROM ActionDescription WHERE HumanReadableName='" + rows.get(i).get(1)+ "'");
			ScenarioActionDescription action = new ScenarioActionDescription(rows.get(i).get(1), singleAction.firstElement().get(1), singleAction.firstElement().get(2), rows.get(i).get(3));
			actions.add(action);
		}
		return actions;
	}
	
	private static Vector<String> getServicesForHost(int hostId){
		Vector<Vector<String>> rows = AccessConnect.executeSelect("SELECT Service FROM HostServices WHERE HostID=" + hostId);
		Vector<String> services = new Vector <String>();
		for (int i=0; i < rows.size(); i++){
			services.add(rows.get(i).firstElement());
		}
		return services;
	}
	
	public static String getUSID (){
		Vector<Vector<String>> temp = AccessConnect.executeSelect("SELECT MAX(USID)+1 FROM " + CommonConstants.simulationExecutionTable);
		try {
			if (temp != null){
				return temp.firstElement().firstElement();	
			}
			return null;
		} catch (NumberFormatException e) {
			return null;
		}
	}
}
